package reduce.fz.com.reduce.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.TypeReference;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

import reduce.fz.com.reduce.R;
import reduce.fz.com.reduce.abstracts.AbResponseListener;
import reduce.fz.com.reduce.bean.Team;
import reduce.fz.com.reduce.bean.result.FindTeamItem;
import reduce.fz.com.reduce.bean.result.Result;
import reduce.fz.com.reduce.custom.HeaderFooterStatusRecyclerViewAdapter;
import reduce.fz.com.reduce.service.FindService;
import reduce.fz.com.reduce.util.PicassoUtil;
import reduce.fz.com.reduce.util.ServiceBuilder;
import reduce.fz.com.reduce.util.UserUtil;

/**
 * Created by qingshengzheng on 15/7/27.
 */
public class FindTeamAdapter extends HeaderFooterStatusRecyclerViewAdapter<FindTeamAdapter.FindTeamViewHolder> {

    private List<Team> teamItems=new ArrayList<Team>();
    private LayoutInflater layoutInflater;
    private Context context;
    private int headerLayid;
    private int contentLayid = R.layout.team_card_view;
    private TeamClickListener teamClickListener;
    private FindService findService = ServiceBuilder.builderFindService(null);

    public FindTeamAdapter(Context context, List<Team> iteams, TeamClickListener teamClickListener) {
        this.context = context;
        this.teamClickListener = teamClickListener;
        if(iteams!=null) {
            this.teamItems = iteams;
        }
        this.layoutInflater = LayoutInflater.from(context);
    }


    public void clearData() {
        teamItems.clear();
        notifyDataSetChanged();
    }

    public void initTeams(List<Team> teams) {
        teams.clear();
        this.teamItems.addAll(teams);
        notifyDataSetChanged();
    }

    public void addTeams(List<Team> teams) {
        int start = teamItems.size();
        int limit = teams.size();
        this.teamItems.addAll(teams);
        notifyContentItemRangeInserted(start, limit);
    }

    @Override
    public FindTeamViewHolder createFooterStatusViewHolder(View footerView) {
        return new FindTeamViewHolder(footerView, this, null);
    }

    @Override
    protected int getHeaderItemCount() {
        return 0;
    }

    @Override
    protected int getContentItemCount() {
        return teamItems.size();
    }

    @Override
    protected FindTeamViewHolder onCreateHeaderItemViewHolder(ViewGroup parent, int headerViewType) {
        return null;
    }

    @Override
    protected FindTeamViewHolder onCreateContentItemViewHolder(ViewGroup parent, int contentViewType) {
        View view = layoutInflater.inflate(R.layout.team_card_view,parent,false);
        return new FindTeamViewHolder(view, this, this.teamClickListener);
    }

    @Override
    protected void onBindHeaderItemViewHolder(FindTeamViewHolder headerViewHolder, int position) {

    }

    @Override
    protected void onBindContentItemViewHolder(FindTeamViewHolder contentViewHolder, int position) {
        contentViewHolder.renderView(this.teamItems.get(position));
    }


    public static class FindTeamViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private ImageView mTeamItemImage;
        private TextView mTeamItemTitle;
        private TextView mTeamItemDesc;
        private RelativeLayout joinIn;
        private TeamClickListener teamClickListener;

        public FindTeamViewHolder(View itemView, RecyclerView.Adapter adapter, TeamClickListener teamClickListener) {
            super(itemView);

            initView(itemView);
        }

        public void renderView(final Team teamitem) {
            Picasso.with(itemView.getContext()).load(R.drawable.holdon).into(mTeamItemImage);
            mTeamItemTitle.setTag(teamitem.getTeamid());
            mTeamItemImage.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    teamClickListener.selectTeamDetail(UserUtil.getUser().getUid(), (Long) mTeamItemTitle.getTag());
                }
            });
        }

        public void initView(View itemView) {
            mTeamItemImage = (ImageView) itemView.findViewById(R.id.team_item_image);
            mTeamItemTitle = (TextView) itemView.findViewById(R.id.team_item_title);
            mTeamItemDesc = (TextView) itemView.findViewById(R.id.team_item_desc);
            joinIn = (RelativeLayout) itemView.findViewById(R.id.rl_joinin);
        }

        @Override
        public void onClick(View view) {
            /*switch (view.getId()) {
                case R.id.rl_joinin:
                    teamClickListener.selectTeamDetail(UserUtil.getUser().getUid(), (Long) mTeamItemTitle.getTag());
                    break;
                default:
                    break;
            }*/
        }
    }


    public static interface TeamClickListener {
        public void selectTeamDetail(Long uid, Long teamid);
    }

}